﻿using System;
using System.Drawing;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

public partial class Calculator : Form
{
    private double previousNumber;
    private double currentNumber;
    private string currentOperator;
    private bool isNewInput = true;

    public Calculator()
    {
        InitializeComponent();
        InitializeButtons();
    }

    private void InitializeButtons()
    {
        // 数字按钮
        for (int i = 0; i < 10; i++)
        {
            Button btn = new Button()
            {
                Text = i.ToString(),
                Size = new Size(60, 60),
                Tag = i
            };
            btn.Click += NumberButton_Click;
            tableLayoutPanel1.Controls.Add(btn, i % 3, i / 3 + 1);
        }

        // 运算符按钮
        string[] operators = { "+", "-", "*", "/" };
        for (int i = 0; i < 4; i++)
        {
            Button btn = new Button()
            {
                Text = operators[i],
                Size = new Size(60, 60),
                Tag = operators[i]
            };
            btn.Click += OperatorButton_Click;
            tableLayoutPanel1.Controls.Add(btn, 3, i);
        }

        // 等号按钮
        Button equalsBtn = new Button()
        {
            Text = "=",
            Size = new Size(60, 122),
            Tag = "="
        };
        equalsBtn.Click += EqualsButton_Click;
        tableLayoutPanel1.Controls.Add(equalsBtn, 3, 4);

        // 清空按钮
        Button clearBtn = new Button()
        {
            Text = "C",
            Size = new Size(124, 60),
            Tag = "C"
        };
        clearBtn.Click += ClearButton_Click;
        tableLayoutPanel1.Controls.Add(clearBtn, 0, 4, 2, 1);
    }

    private void NumberButton_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        int number = (int)btn.Tag;

        if (isNewInput)
        {
            textBox1.Text = number.ToString();
            isNewInput = false;
        }
        else
        {
            textBox1.Text += number.ToString();
        }

        currentNumber = double.Parse(textBox1.Text);
    }

    private void OperatorButton_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string op = (string)btn.Tag;

        if (!string.IsNullOrEmpty(currentOperator))
        {
            Calculate();
        }

        previousNumber = currentNumber;
        currentOperator = op;
        isNewInput = true;
    }

    private void EqualsButton_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(currentOperator))
        {
            Calculate();
            textBox1.Text = $"{previousNumber}{currentOperator}{currentNumber}={textBox1.Text}";
            currentOperator = string.Empty;
            isNewInput = true;
        }
    }

    private void Calculate()
    {
        try
        {
            switch (currentOperator)
            {
                case "+":
                    currentNumber = previousNumber + currentNumber;
                    break;
                case "-":
                    currentNumber = previousNumber - currentNumber;
                    break;
                case "*":
                    currentNumber = previousNumber * currentNumber;
                    break;
                case "/":
                    if (currentNumber == 0)
                    {
                        MessageBox.Show("除数不能为零！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    currentNumber = previousNumber / currentNumber;
                    break;
            }
            textBox1.Text = currentNumber.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show($"计算错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            ClearAll();
        }
    }

    private void ClearButton_Click(object sender, EventArgs e)
    {
        ClearAll();
    }

    private void ClearAll()
    {
        previousNumber = 0;
        currentNumber = 0;
        currentOperator = string.Empty;
        textBox1.Text = string.Empty;
        isNewInput = true;
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Calculator());
    }
}
